// var n;
// exports.DebugViewManager = exports.AL_Framework_Debug_View = void 0;
// var $logger = require("./AL_Framework_Logger");
// var $macros = require("./AL_Framework_Macros");
// var l = cc._decorator;
// var d = l.ccclass;
// var u =
//     (l.property,
//     (function (e) {
//         function t() {
//             var t = (null !== e && e.apply(this, arguments)) || this;
//             t.node_log_view = null;
//             t.content = null;
//             t.debug = null;
//             return t;
//         }
//         __extends(t, e);
//         t.prototype.onLoad = function () {
//             this.content = cc.find("content", this.node);
//             this.bangdingShijian("showRes", this.on_Show_Res);
//             this.bangdingShijian("showDebugInfo", this.on_Show_Debug_Info);
//             this.bangdingShijian("log", this.on_Log);
//             this.bangdingShijian("proto", this.on_Proto);
//             this.bangdingShijian("bundleMgr", this.on_Bundle_Mgr);
//             this.bangdingShijian("netHelper", this.on_Net_Helper);
//             this.bangdingShijian("serviceManager", this.on_Service_Manager);
//             this.bangdingShijian("releaseManager", this.on_Release_Manager);
//             this.bangdingShijian("adaptor", this.on_Adaptor);
//             this.doOther();
//         };
//         t.prototype.doOther = function () {
//             var e = this;
//             var t = cc.find("node_log_view", this.node);
//             if (t) {
//                 t.active = !1;
//                 this.node_log_view = t;
//                 this.initLogView();
//             }
//             var o = cc.find("background", this.node);
//             if (o) {
//                 o.on(cc.Node.EventType.TOUCH_END, function () {
//                     e.node.active = !1;
//                     if (e.debug) {
//                         e.debug.active = !0;
//                     }
//                 });
//             }
//         };
//         t.prototype.bangdingShijian = function (e, t) {
//             var o = cc.find(e, this.content);
//             if (o) {
//                 o.on(cc.Node.EventType.TOUCH_END, t, this);
//             }
//         };
//         t.prototype.initLogView = function () {
//             var e = this;
//             var t = cc.find("background", this.node_log_view);
//             if (t) {
//                 t.on(cc.Node.EventType.TOUCH_END, function () {
//                     e.node_log_view.active = !1;
//                 });
//             }
//             var o = cc.find("level", this.node_log_view);
//             if (o) {
//                 var i = function (t) {
//                     var i = cc.find("type" + t, o);
//                     if (i) {
//                         var a = i.getComponent(cc.Toggle);
//                         if (a) {
//                             a.isChecked = Manager.logger.isValid(n.huoquRizhiJibie(t));
//                         }
//                         i.on("toggle", function (o) {
//                             o.isChecked
//                                 ? Manager.logger.attach(e.huoquRizhiJibie(t))
//                                 : Manager.logger.detach(e.huoquRizhiJibie(t));
//                         });
//                     }
//                 };
//                 var n = this;
//                 for (var a = 0; a < o.children.length - 1; a++) {
//                     i(a);
//                 }
//             }
//         };
//         t.prototype.huoquRizhiJibie = function (e) {
//             switch (e) {
//                 case 0:
//                     return $logger.LogLevel.DEBUG;
//                 case 1:
//                     return $logger.LogLevel.WARN;
//                 case 2:
//                     return $logger.LogLevel.ERROR;
//                 case 3:
//                     return $logger.LogLevel.DUMP;
//                 default:
//                     return $logger.LogLevel.DEBUG;
//             }
//         };
//         t.prototype.on_Proto = function () {};
//         t.prototype.on_Bundle_Mgr = function () {
//             Log.d("-------Bundle管理器状态信息-------");
//             Manager.bundleManager.print({
//                 print: function (e) {
//                     var t = [];
//                     for (var o = 0; o < e.loaded.length; o++) {
//                         t.push(e.loaded[o].name);
//                     }
//                     Log.d("当前所有加载完成的bundle : " + t.toString());
//                 }
//             });
//         };
//         t.prototype.on_Log = function () {
//             this.node_log_view.active = !0;
//         };
//         t.prototype.on_Show_Debug_Info = function () {
//             cc.debug.setDisplayStats(!cc.debug.isDisplayStats());
//         };
//         t.prototype.on_Show_Res = function () {
//             Manager.cacheManager.print({
//                 printLocal: function (e) {
//                     var t = [];
//                     var o = [];
//                     e.forEach(function (e, i) {
//                         var n = {
//                             url: i,
//                             data: {
//                                 url: e.info.url,
//                                 isLoaded: e.isLoaded,
//                                 retain: e.info.retain,
//                                 isValid: cc.isValid(e.data),
//                                 assetType: cc.js.getClassName(e.info.type),
//                                 data: e.data ? cc.js.getClassName(e.data) : null,
//                                 status: e.status
//                             }
//                         };
//                         e.isLoaded && e.data && !cc.isValid(e.data) ? o.push(n) : t.push(n);
//                     });
//                     if (t.length > 0) {
//                         Log.d("----------- 有效缓存信息 -----------");
//                         Log.d(JSON.stringify(t));
//                     }
//                     if (o.length > 0) {
//                         Log.d("----------- 无效缓存信息 -----------");
//                         Log.d(JSON.stringify(o));
//                     }
//                 },
//                 printRemote: function (e, t, o) {
//                     Log.d("---- 远程加载资源缓存信息 ----");
//                     var i = [];
//                     var n = [];
//                     e.forEach(function (e, t) {
//                         var o = {
//                             url: t,
//                             data: {
//                                 url: e.info.url,
//                                 isLoaded: e.isLoaded,
//                                 isValid: cc.isValid(e.data),
//                                 assetType: cc.js.getClassName(e.info.type),
//                                 data: e.data ? cc.js.getClassName(e.data) : null,
//                                 status: e.status
//                             }
//                         };
//                         e.isLoaded && ((e.data && !cc.isValid(e.data)) || !e.data) ? n.push(o) : i.push(o);
//                     });
//                     if (i.length > 0) {
//                         Log.d("----------------有效 spriteFrame 缓存信息------------------");
//                         Log.d(JSON.stringify(i));
//                     }
//                     if (n.length > 0) {
//                         Log.d("----------------无效 spriteFrame 缓存信息------------------");
//                         Log.d(JSON.stringify(n));
//                     }
//                     i = [];
//                     n = [];
//                     t.forEach(function (e, t) {
//                         var o = {
//                             url: t,
//                             data: {
//                                 url: e.info.url,
//                                 isLoaded: e.isLoaded,
//                                 isValid: cc.isValid(e.data),
//                                 assetType: cc.js.getClassName(e.info.type),
//                                 data: e.data ? cc.js.getClassName(e.data) : null,
//                                 status: e.status
//                             }
//                         };
//                         e.isLoaded && e.data && !cc.isValid(e.data) ? n.push(o) : i.push(o);
//                     });
//                     if (i.length > 0) {
//                         Log.d("----------------有效缓存信息------------------");
//                         Log.d(JSON.stringify(i));
//                     }
//                     if (n.length > 0) {
//                         Log.d("----------------无效缓存信息------------------");
//                         Log.d(JSON.stringify(n));
//                     }
//                     if (o.size > 0) {
//                         Log.d("----------------当前资源引用计数信息------------------");
//                         i = [];
//                         o.forEach(function (e, t) {
//                             var o = {
//                                 url: t,
//                                 data: {
//                                     refCount: e.refCount,
//                                     url: e.url,
//                                     retain: e.retain
//                                 }
//                             };
//                             i.push(o);
//                         });
//                         Log.d(JSON.stringify(i));
//                     }
//                 }
//             });
//         };
//         t.prototype.on_Net_Helper = function () {};
//         t.prototype.on_Service_Manager = function () {};
//         t.prototype.on_Release_Manager = function () {
//             Manager.releaseManager.print({
//                 print: function (e) {
//                     Manager.shifou_yanchi_shifang_ziyuan
//                         ? (e.bundles.length > 0 && Log.d("待释放Bundle : " + e.bundles.toString()),
//                           e.lazyInfo.size > 0 &&
//                               e.lazyInfo.forEach(function (e, t) {
//                                   Log.d("--------------" + t + "待释放资源--------------");
//                                   e.assets.forEach(function (e) {
//                                       Log.d("" + e.url);
//                                   });
//                               }),
//                           Log.d("远程待释放资源"),
//                           e.remote.assets.forEach(function (e) {
//                               Log.d("" + e.url);
//                           }))
//                         : Log.w("未开户懒释放功能!!!!");
//                 }
//             });
//         };
//         t.prototype.on_Adaptor = function () {
//             Log.d("-----------------------------适配信息-----------------------------------------------");
//             Log.d("屏幕分辨率: " + cc.view.getCanvasSize().width + " x " + cc.view.getCanvasSize().height);
//             Log.d(
//                 "视图窗口可见区域分辨率: " + cc.view.getVisibleSize().width + " x " + cc.view.getVisibleSize().height
//             );
//             Log.d("视图中边框尺寸: " + cc.view.getFrameSize().width + " x " + cc.view.getFrameSize().height);
//             Log.d("设备或浏览器像素比例: " + cc.view.getDevicePixelRatio());
//             Log.d(
//                 "返回视图窗口可见区域像素尺寸: " +
//                     cc.view.getVisibleSizeInPixel().width +
//                     " x " +
//                     cc.view.getVisibleSizeInPixel().height
//             );
//             Log.d(
//                 "当前场景设计分辨率: " +
//                     cc.view.getDesignResolutionSize().width +
//                     " x " +
//                     cc.view.getDesignResolutionSize().height
//             );
//             var e = cc.view.getFrameSize().width / cc.view.getFrameSize().height;
//             var t = cc.view.getDesignResolutionSize().width / cc.view.getDesignResolutionSize().height;
//             Log.d("视图宽高比:" + e);
//             Log.d("设置分辨率宽高比:" + t);
//         };
//         return __decorate([d], t);
//     })(cc.Component));
// exports.AL_Framework_Debug_View = u;
// var h = (function () {
//     function e() {
//         this.debugView = null;
//     }
//     e.Instance = function () {
//         return this._instance || (this._instance = new e());
//     };
//     e.prototype.showDebug = function () {
//         var e = this;
//         return new Promise(function (t, o) {
//             if (e.debugView) {
//                 e.debugView.active = !0;
//                 return Promise.resolve();
//             }
//             Manager.cacheManager
//                 .yibuHuoquHuancun(
//                     "PPFrameWork/framework/common/components/debug/debugView",
//                     cc.Prefab,
//                     $macros.Macro.BUNDLE_RESOURCES
//                 )
//                 .then(function (i) {
//                     if (i) {
//                         var n = cc.instantiate(i);
//                         Manager.uiManager.tianjia_shitu(n, 50);
//                         e.debugView = n;
//                         t(i);
//                     } else {
//                         o();
//                     }
//                 });
//         });
//     };
//     e._instance = null;
//     return e;
// })();
// exports.DebugViewManager = h;


const { ccclass, property } = cc._decorator;
const $logger = require("./AL_Framework_Logger");
const $macros = require("./AL_Framework_Macros");

let logManager;

/**
 * DebugView类，负责管理调试视图和事件绑定
 */
@ccclass
class AL_Framework_Debug_View extends cc.Component {
    @property(cc.Node)
    node_log_view = null;

    @property(cc.Node)
    content = null;

    @property(cc.Node)
    debug = null;

    /**
     * 生命周期函数，加载时触发
     */
    onLoad() {
        this.content = cc.find("content", this.node);
        this.bangdingShijian("showRes", this.on_Show_Res);
        this.bangdingShijian("showDebugInfo", this.on_Show_Debug_Info);
        this.bangdingShijian("log", this.on_Log);
        this.bangdingShijian("proto", this.on_Proto);
        this.bangdingShijian("bundleMgr", this.on_Bundle_Mgr);
        this.bangdingShijian("netHelper", this.on_Net_Helper);
        this.bangdingShijian("serviceManager", this.on_Service_Manager);
        this.bangdingShijian("releaseManager", this.on_Release_Manager);
        this.bangdingShijian("adaptor", this.on_Adaptor);
        this.initializeOtherSettings();
    }

    /**
     * 初始化其它设置
     */
    initializeOtherSettings() {
        const logNode = cc.find("node_log_view", this.node);
        if (logNode) {
            logNode.active = false;
            this.node_log_view = logNode;
            this.initialize_Log_View();
        }

        const backgroundNode = cc.find("background", this.node);
        if (backgroundNode) {
            backgroundNode.on(cc.Node.EventType.TOUCH_END, () => {
                this.node.active = false;
                if (this.debug) {
                    this.debug.active = true;
                }
            });
        }
    }

    /**
     * 绑定事件
     * @param {string} eventName 事件名称
     * @param {Function} handler 处理函数
     */
    bangdingShijian(eventName, handler) {
        const eventNode = cc.find(eventName, this.content);
        if (eventNode) {
            eventNode.on(cc.Node.EventType.TOUCH_END, handler, this);
        }
    }

    /**
     * 初始化日志视图
     */
    initialize_Log_View() {
        const logBackground = cc.find("background", this.node_log_view);
        if (logBackground) {
            logBackground.on(cc.Node.EventType.TOUCH_END, () => {
                this.node_log_view.active = false;
            });
        }

        const logLevelNode = cc.find("level", this.node_log_view);
        if (logLevelNode) {
            for (let i = 0; i < logLevelNode.children.length - 1; i++) {
                this.setup_Log_Level_Toggle(i, logLevelNode);
            }
        }
    }

    /**
     * 设置日志级别切换
     * @param {number} index 日志级别索引
     * @param {cc.Node} logLevelNode 日志级别节点
     */
    setup_Log_Level_Toggle(index, logLevelNode) {
        const typeNode = cc.find("type" + index, logLevelNode);
        if (typeNode) {
            const toggleComponent = typeNode.getComponent(cc.Toggle);
            if (toggleComponent) {
                toggleComponent.isChecked = Manager.logger.isValid(this.huoquRizhiJibie(index));
            }
            typeNode.on("toggle", (event) => {
                event.isChecked
                    ? Manager.logger.attach(this.huoquRizhiJibie(index))
                    : Manager.logger.detach(this.huoquRizhiJibie(index));
            });
        }
    }

    /**
     * 获取日志级别
     * @param {number} level 日志级别索引
     * @returns {string} 日志级别
     */
    huoquRizhiJibie(level) {
        switch (level) {
            case 0:
                return $logger.LogLevel.DEBUG;
            case 1:
                return $logger.LogLevel.WARN;
            case 2:
                return $logger.LogLevel.ERROR;
            case 3:
                return $logger.LogLevel.DUMP;
            default:
                return $logger.LogLevel.DEBUG;
        }
    }

    // 空方法，可以根据需要实现
    on_Proto() {}

    on_Bundle_Mgr() {
        Log.d("-------Bundle管理器状态信息-------");
        Manager.bundleManager.print({
            print: (data) => {
                const loadedBundles = data.loaded.map(bundle => bundle.name);
                Log.d("当前所有加载完成的bundle : " + loadedBundles.toString());
            }
        });
    }

    on_Log() {
        this.node_log_view.active = true;
    }

    on_Show_Debug_Info() {
        cc.debug.setDisplayStats(!cc.debug.isDisplayStats());
    }

    on_Show_Res() {
        Manager.cacheManager.print({
            printLocal: (localCache) => {
                const validCache = [];
                const invalidCache = [];
                localCache.forEach((value, key) => {
                    const cacheItem = {
                        url: key,
                        data: {
                            url: value.info.url,
                            isLoaded: value.isLoaded,
                            retain: value.info.retain,
                            isValid: cc.isValid(value.data),
                            assetType: cc.js.getClassName(value.info.type),
                            data: value.data ? cc.js.getClassName(value.data) : null,
                            status: value.status
                        }
                    };
                    value.isLoaded && value.data && !cc.isValid(value.data) ? invalidCache.push(cacheItem) : validCache.push(cacheItem);
                });
                if (validCache.length) {
                    Log.d("----------- 有效缓存信息 -----------");
                    Log.d(JSON.stringify(validCache));
                }
                if (invalidCache.length) {
                    Log.d("----------- 无效缓存信息 -----------");
                    Log.d(JSON.stringify(invalidCache));
                }
            },
            printRemote: (remoteCache, spriteFrames, references) => {
                const processCache = (cache, valid, invalid) => {
                    cache.forEach((value, key) => {
                        const cacheItem = {
                            url: key,
                            data: {
                                url: value.info.url,
                                isLoaded: value.isLoaded,
                                isValid: cc.isValid(value.data),
                                assetType: cc.js.getClassName(value.info.type),
                                data: value.data ? cc.js.getClassName(value.data) : null,
                                status: value.status
                            }
                        };
                        value.isLoaded && ((value.data && !cc.isValid(value.data)) || !value.data) ? invalid.push(cacheItem) : valid.push(cacheItem);
                    });
                };

                let validSpriteFrames = [];
                let invalidSpriteFrames = [];
                processCache(remoteCache, validSpriteFrames, invalidSpriteFrames);

                if (validSpriteFrames.length) {
                    Log.d("----------------有效 spriteFrame 缓存信息------------------");
                    Log.d(JSON.stringify(validSpriteFrames));
                }
                if (invalidSpriteFrames.length) {
                    Log.d("----------------无效 spriteFrame 缓存信息------------------");
                    Log.d(JSON.stringify(invalidSpriteFrames));
                }

                validSpriteFrames = [];
                invalidSpriteFrames = [];
                processCache(spriteFrames, validSpriteFrames, invalidSpriteFrames);

                if (validSpriteFrames.length) {
                    Log.d("----------------有效缓存信息------------------");
                    Log.d(JSON.stringify(validSpriteFrames));
                }
                if (invalidSpriteFrames.length) {
                    Log.d("----------------无效缓存信息------------------");
                    Log.d(JSON.stringify(invalidSpriteFrames));
                }

                if (references.size) {
                    Log.d("----------------当前资源引用计数信息------------------");
                    const referenceData = [];
                    references.forEach((value, key) => {
                        referenceData.push({
                            url: key,
                            data: {
                                refCount: value.refCount,
                                url: value.url,
                                retain: value.retain
                            }
                        });
                    });
                    Log.d(JSON.stringify(referenceData));
                }
            }
        });
    }

    // 空方法，可以根据需要实现
    on_Net_Helper() {}

    // 空方法，可以根据需要实现
    on_Service_Manager() {}

    on_Release_Manager() {
        Manager.releaseManager.print({
            print: (data) => {
                if (Manager.shifou_yanchi_shifang_ziyuan) {
                    if (data.bundles.length) {
                        Log.d("待释放Bundle : " + data.bundles.toString());
                    }
                    if (data.lazyInfo.size) {
                        data.lazyInfo.forEach((assets, bundleName) => {
                            Log.d("--------------" + bundleName + "待释放资源--------------");
                            assets.forEach(asset => {
                                Log.d("" + asset.url);
                            });
                        });
                    }
                    Log.d("远程待释放资源");
                    data.remote.assets.forEach(asset => {
                        Log.d("" + asset.url);
                    });
                } else {
                    Log.w("未开户懒释放功能!!!!");
                }
            }
        });
    }

    on_Adaptor() {
        Log.d("-----------------------------适配信息-----------------------------------------------");
        Log.d("屏幕分辨率: " + cc.view.getCanvasSize().width + " x " + cc.view.getCanvasSize().height);
        Log.d("视图窗口可见区域分辨率: " + cc.view.getVisibleSize().width + " x " + cc.view.getVisibleSize().height);
        Log.d("视图中边框尺寸: " + cc.view.getFrameSize().width + " x " + cc.view.getFrameSize().height);
        Log.d("设备或浏览器像素比例: " + cc.view.getDevicePixelRatio());
        Log.d("返回视图窗口可见区域像素尺寸: " + cc.view.getVisibleSizeInPixel().width + " x " + cc.view.getVisibleSizeInPixel().height);
        Log.d("当前场景设计分辨率: " + cc.view.getDesignResolutionSize().width + " x " + cc.view.getDesignResolutionSize().height);
        const screenAspectRatio = cc.view.getFrameSize().width / cc.view.getFrameSize().height;
        const designAspectRatio = cc.view.getDesignResolutionSize().width / cc.view.getDesignResolutionSize().height;
        Log.d("视图宽高比:" + screenAspectRatio);
        Log.d("设置分辨率宽高比:" + designAspectRatio);
    }
}

exports.AL_Framework_Debug_View = AL_Framework_Debug_View;

/**
 * DebugViewManager类，负责管理DebugView实例
 */
class DebugViewManager {
    constructor() {
        this.debugView = null;
    }

    static getInstance() {
        if (!this._instance) {
            this._instance = new DebugViewManager();
        }
        return this._instance;
    }

    showDebug() {
        return new Promise((resolve, reject) => {
            if (this.debugView) {
                this.debugView.active = true;
                return resolve();
            }
            Manager.cacheManager
                .yibuHuoquHuancun(
                    "PPFrameWork/framework/common/components/debug/debugView",
                    cc.Prefab,
                    $macros.Macro.BUNDLE_RESOURCES
                )
                .then((prefab) => {
                    if (prefab) {
                        const instance = cc.instantiate(prefab);
                        Manager.uiManager.tianjia_shitu(instance, 50);
                        this.debugView = instance;
                        resolve(prefab);
                    } else {
                        reject();
                    }
                });
        });
    }
}

DebugViewManager._instance = null;
exports.DebugViewManager = DebugViewManager;
